import {
  createParams,
  detailItem,
  detailParams,
  editParams,
  enableParams,
  routeLinkItem,
  ruleItem,
  ruleParams,
} from './model/ruleManagementModel';
import { upgradeHttp } from '/@/utils/http/axios';

enum Api {
  GET_RULE_TABLE = '/v1/routeLinkRule/list',
  GET_ROUTE_LINK_LIST = '/v1/routeLinkRule/routeLinkKeys',
  CREATE_RULE = '/v1/routeLinkRule/create',
  EDIT_RULE = '/v1/routeLinkRule/update',
  RULE_DETAIL = '/v1/routeLinkRule/detail',
  DELETE_RULE = '/v1/routeLinkRule/delete',
  ENABLE_RULE = '/v1/routeLinkRule/enable',
}

//获取规则表格
export const getRuleTable = async (params: ruleParams) => {
  const res = await upgradeHttp.post<ruleItem[]>({
    url: Api.GET_RULE_TABLE,
    params,
  });
  const result = res.map((item) => {
    if (item.status == true) {
      item.showStatus = '启用';
    } else if (item.status == false) {
      item.showStatus = '停用';
    }
    return item;
  });
  return result;
};
//获取路由链路，所属链路
export const getRouteLink = async () => {
  const res = await upgradeHttp.get<routeLinkItem[]>({
    url: Api.GET_ROUTE_LINK_LIST,
  });
  const result = res.map((item) => {
    return {
      label: item.value,
      value: item.key,
    };
  });
  return result;
};
//创建规则
export const createRule = async (params: createParams) => {
  const res = await upgradeHttp.post<string>({
    url: Api.CREATE_RULE,
    params,
  });
  return res;
};
//编辑规则
export const editRule = async (params: editParams) => {
  const res = await upgradeHttp.put<string>({
    url: Api.EDIT_RULE,
    params,
  });
  return res;
};
//查看详情
export const getRuleDetail = async (params: detailParams) => {
  const _params = { routeLinkRuleId: params };
  const res = await upgradeHttp.get<detailItem>({
    url: Api.RULE_DETAIL,
    params: _params,
  });
  if (res.status == true) {
    res.showStatus = '启用';
  } else if (res.status == false) {
    res.showStatus = '停用';
  }
  return res;
};
//停启用
export const enableRule = async (params: enableParams) => {
  const res = await upgradeHttp.put<string>({
    url: Api.ENABLE_RULE,
    params,
  });
  return res;
};
//删除
export const deleteRule = async (params: detailParams) => {
  const _param = params;
  const res = await upgradeHttp.delete<string>({
    url: Api.DELETE_RULE + '?routeLinkRuleId=' + _param,
  });
  return res;
};
